<template>
	<view class="product-manage platform-batch-manage batch-manage">
		<scroll-view class="product-manage-view" scroll-y @scrolltolower="loadMore">
			<view class="product-manage-list" v-if="productList.length">
				<checkbox-group @change="change">
					<view class="product-manage-item" v-for="(data,index) in productList" :key="index">
						<label>
							<checkbox :value="(data.goods_id).toString()" :checked="data.checked" color="#FFF"/>
							<image class="product-img" :src="data.image_middle" mode="aspectFill"></image>
							<view class="product-manage-info">
								<view class="product-manage-info-tit">{{data.goods_name}}</view>
								<view class="product-manage-info-text">零售价：¥{{data.retail_price}}</view>
								<view class="product-manage-info-text">成本价：¥{{data.self_price}}</view>
								<view class="product-manage-info-text"><text>人气：<text>{{data.click}}</text></text></view>
							</view>
						</label>
					</view>
				</checkbox-group>
			</view>
			<nothing :text="'没有商品'" :btn="false" v-if="!productList.length"></nothing>
			<no-more :text="'数据加载完毕'" v-if="productList.length && noMore"></no-more>
		</scroll-view>
		
		<view class="all-chose" v-if="productList.length">
			<checkbox-group @change="changeAll"><label><checkbox :checked="allChecked" color="#FFF"/>全选</label></checkbox-group>
		</view>
		<view class="product-manage-bottom" v-if="productList.length">
			<view @click="downPro">下架商品</view>
			<view @click="delPro">删除商品</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				allChecked: false,
				noMore: false,
				page: 0,
				categoryID: '',
				productList: [],
				status: 1
			};
		},
		onLoad(options) {
			if(options.type){
				this.categoryID = options.type
			}
			if(options.status){
				this.status = options.status
			}
			this.init()
		},
		onShow() {
			
		},
		methods: {
			init(page){
				let data = {
					limit: 10,
					page: page?page:0,
					platform_class_id: this.categoryID,
					status: this.status,
					store_id: JSON.parse(uni.getStorageSync('userInfo')).store_id,
				}
				this.$api.PlatformGoodsLists(data).then(res => {
					if(res.datas.length){
						res.datas.forEach(item => {
							this.productList.push(item)
						})
					}else{
						this.noMore = true
					}
				})
			},
			loadMore(){
				if(!this.noMore){
					this.page++
					this.init(this.page)
				}
			},
			change(e){
				let _this = this
				var items = this.productList,
						values = e.detail.value;
				for (var i = 0, lenI = items.length; i < lenI; ++i) {
					const item = items[i]
					if(values.includes(item.goods_id.toString())){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
			},
			changeAll(e){
				if(e.detail.value.length){
					this.allChecked = true
					this.productList.forEach((item,index) => {
						this.$set(item,'checked',true)
					})
				}else{
					this.allChecked = false
					this.productList.forEach((item,index) => {
						this.$set(item,'checked',false)
					})
				}
			},
			downPro(){
				let ids = []
				this.productList.forEach(item => {
					if(item.checked){
						ids.push(item.store_platform_id)
					}
				})
				if(!ids.length){
					uni.showToast({
						icon: 'none',
						title: '至少选择一条商品'
					})
					return false
				}
				let _this = this
				uni.showModal({
					title: '温馨提示',
					content: '是否确认下架所选商品?',
					success(res) {
						if(res.confirm){
							_this.$api.PlatformOutGoods({status: 2,store_platform_id: ids.join(',')}).then(res => {
								uni.showToast({
									icon: "none",
									title: "下架成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.allChecked = false
									_this.productList = []
									_this.init()
								},1500)
							})
						}
					}
				})
			},
			delPro(){
				let ids = []
				this.productList.forEach(item => {
					if(item.checked){
						ids.push(item.store_platform_id)
					}
				})
				if(!ids.length){
					uni.showToast({
						icon: 'none',
						title: '至少选择一条商品'
					})
					return false
				}
				let _this = this
				uni.showModal({
					title: '温馨提示',
					content: '是否确认删除所选商品?',
					success(res) {
						if(res.confirm){
							_this.$api.PlatformDelateGoods({store_platform_id: ids.join(',')}).then(res => {
								uni.showToast({
									icon: "none",
									title: "删除成功",
									duration: 1500
								})
								setTimeout(() => {
									_this.allChecked = false
									_this.productList = []
									_this.init()
								},1500)
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '../../static/scss/product-manage.scss';
	.platform-batch-manage{
		.product-manage-view{
			top: 0;
			bottom: 180rpx;
			label{
				display: flex;
				align-items: center;
			}
			.product-manage-info-tit{
				height: 92rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}
		}
		.all-chose{
			position: fixed;
			left: 0;
			right: 0;
			bottom: 90rpx;
			height: 90rpx;
			padding: 20rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.1);
			label{
				display: flex;
				align-items: center;
			}
		}
	}
</style>